//
//  WmcHudManager.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/3.
//

import CoreGraphics
import SVProgressHUD
import Toast_Swift
import UIKit
import YPBedrock
import YPNetwork

class WmcHudManager: NSObject {
    /// 单例
    static let shared: WmcHudManager = {
        let manager = WmcHudManager()
        return manager
    }()

    /// 展示加载
    static func loading() {
//        SVProgressHUD.show()
    }

    static func dismiss() {
        SVProgressHUD.dismiss()
    }
    
    static let defaultMsg = "网络异常，请稍后重试"

    /// 提示文案
    static func toast(_ msg: String, duration: TimeInterval = 1.3, position: ToastPosition = .center, textAlignment: NSTextAlignment = .center) {
        YPGCD.Main {
            var toastStyle = ToastStyle()
            toastStyle.messageAlignment = textAlignment
            toastStyle.maxWidthPercentage = 0.7
            YPApp.KeyWindow?
                .makeToast(msg,
                           duration: duration,
                           position: position,
                           title: nil,
                           image: nil,
                           style: toastStyle,
                           completion: nil)
        }
    }

    static func toast(_ error: Error?, duration: TimeInterval = 1.3, position: ToastPosition = .center) { 
        var msg = error?.localizedDescription ?? defaultMsg

        if let netError = error as? YPNetworkError,
           let netErrorMsg = netError.localizedDescription {
            if YPNetworkReachability.queryNetworkReachabilityType() == .notReachable {
                msg = defaultMsg
            } else if netErrorMsg == "请求失败： 似乎已断开与互联网的连接。" {
                msg = defaultMsg
            } else {
                msg = netErrorMsg
            }
            if netError.customErrorCode == 40002 {
                return
            }
        }

        YPGCD.Main {
            YPApp.KeyWindow?
                .makeToast(msg,
                           duration: duration,
                           position: position,
                           title: nil,
                           image: nil,
                           style: ToastStyle(),
                           completion: nil)
        }
    }
    
    static func defaultToast() {
        toast(defaultMsg)
    }
}
